#include "SDLTime.h"

#include <SDL/SDL.h>

namespace GenBen {
	
SDLTime::SDLTime()
{
	fps_limit = current_time_ms = 0;
	last_fps_update = fps = frames_drawn = 0;
	update();
}

SDLTime::~SDLTime()
{
}

string SDLTime::timeToString(uint time)
{
	char time_str[8];
	sprintf(time_str, "%02u:%02u", time / (1000 * 60), (time / 1000) % 60);
	return string(time_str);
}

void SDLTime::update()
{
	old_time_ms = current_time_ms;

	do
	{
		current_time_ms = SDL_GetTicks();
		d_time_ms = current_time_ms - old_time_ms;
	} while(fps_limit != 0 && (d_time_ms < (1000 / fps_limit)));

	time_scale = (float)d_time_ms / 1000.0f;
	if (last_fps_update + 1000 <= current_time_ms)
	{
		fps = frames_drawn;
		frames_drawn = 0;
		last_fps_update = current_time_ms;
	}
	frames_drawn++;
}

} // namespace
